package cn.cyl.order.service.impl;

import cn.cyl.basic.service.impl.BaseServiceImpl;
import cn.cyl.basic.util.LoginContext;
import cn.cyl.basic.util.PageList;
import cn.cyl.order.domain.OrderPetAcquisition;
import cn.cyl.order.dto.OrderDto;
import cn.cyl.order.mapper.OrderPetAcquisitionMapper;
import cn.cyl.order.query.OrderPetAcquisitionQuery;
import cn.cyl.order.service.OrderPetAcquisitionService;
import cn.cyl.org.domain.Employee;
import cn.cyl.org.domain.Shop;
import cn.cyl.org.mapper.EmployeeMapper;
import cn.cyl.org.mapper.ShopMapper;
import cn.cyl.pet.domain.Pet;
import cn.cyl.pet.domain.SearchMasterMsg;
import cn.cyl.pet.mapper.PetMapper;
import cn.cyl.pet.mapper.SearchMasterMsgMapper;
import cn.cyl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadBylogininfoId(logininfo.getId());
        if(employee.getShop_id()!=null){//店铺管理员
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.loadOne(employee.getShop_id());
            if(shop.getAdminId()!=employee.getId()){//店员
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }


    //订单管理中取消订单的业务实现
    @Override
    //放入寻主池
    public void cancel(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //修改订单的状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);


    }
    //订单管理中确认订单的业务实现
    @Override
    public void confirm(OrderDto dto) {
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();//最终成交价
        Integer payType = dto.getPayType();//支付方式
        //生成宠物对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadOne(order.getSearchMasterMsgId());
        Pet pet=createPet(msg,money);
        petMapper.save(pet);

        //修改订单状态，修改金额 最终成交价
        order.setState(1);
        order.setPrice(money);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //修改寻主消息的状态 state=2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        //pet.setUserId(msg.getUserId());  领养人的Id
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
